package com.hoshiicloud.user.controller.organization;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.organization.StoreOrganizationUsers;
import com.hoshiicloud.user.param.organization.StoreOrganizationUsersAddParam;
import com.hoshiicloud.user.param.organization.StoreOrganizationUsersQueryParam;
import com.hoshiicloud.user.param.organization.StoreOrganizationUsersUpdateParam;
import com.hoshiicloud.user.service.organization.StoreOrganizationUsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 店铺机构用户 控制器
 * </p>
 *
 * @author campchen
 * @since 2019-05-29
 */
@Slf4j
@RestController
@RequestMapping("/organization/store-organization-users")
@Api(value = "平台店铺机构用户", tags = {"平台店铺机构用户"})
@ApiSort(46)
public class StoreOrganizationUsersController {
    @Autowired
    private StoreOrganizationUsersService storeOrganizationUsersService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询店铺机构用户
     */
    @ApiOperation(value = "分页查询店铺机构用户")
    @PostMapping("/getPage")
    public Response page(@RequestBody StoreOrganizationUsersQueryParam param) {
        try {
            QueryWrapper<StoreOrganizationUsers> queryWrapper = new QueryWrapper<StoreOrganizationUsers>();
            queryWrapper.lambda()
                    .eq((param.getMemberId() != null), StoreOrganizationUsers::getMemberId, param.getMemberId())
                    .eq(StringUtils.isNotBlank(param.getUserCode()), StoreOrganizationUsers::getUserCode, param.getUserCode())
                    .eq(StringUtils.isNotBlank(param.getQrCode()), StoreOrganizationUsers::getQrCode, param.getQrCode())
                    .eq(StringUtils.isNotBlank(param.getWorkTelphone()), StoreOrganizationUsers::getWorkTelphone, param.getWorkTelphone())
                    .eq(StringUtils.isNotBlank(param.getPosition()), StoreOrganizationUsers::getPosition, param.getPosition())
                    .eq(StringUtils.isNotBlank(param.getEducational()), StoreOrganizationUsers::getEducational, param.getEducational())
                    .eq(StringUtils.isNotBlank(param.getProfessional()), StoreOrganizationUsers::getProfessional, param.getProfessional())
                    .eq((param.getHireDate() != null), StoreOrganizationUsers::getHireDate, param.getHireDate())
                    .eq(StringUtils.isNotBlank(param.getEmail()), StoreOrganizationUsers::getEmail, param.getEmail())
                    .eq(StringUtils.isNotBlank(param.getOfficeLocation()), StoreOrganizationUsers::getOfficeLocation, param.getOfficeLocation())
                    .eq((param.getLocked() != null), StoreOrganizationUsers::getLocked, param.getLocked())
                    .eq(StringUtils.isNotBlank(param.getWxOpenId()), StoreOrganizationUsers::getWxOpenId, param.getWxOpenId())
                    .eq(StringUtils.isNotBlank(param.getWxUnionId()), StoreOrganizationUsers::getWxUnionId, param.getWxUnionId())
                    .eq(StringUtils.isNotBlank(param.getWxName()), StoreOrganizationUsers::getWxName, param.getWxName())
                    .eq(StringUtils.isNotBlank(param.getWxPic()), StoreOrganizationUsers::getWxPic, param.getWxPic())
                    .eq(StringUtils.isNotBlank(param.getQqOpenId()), StoreOrganizationUsers::getQqOpenId, param.getQqOpenId())
                    .eq((param.getDeleted() != null), StoreOrganizationUsers::getDeleted, param.getDeleted())
                    .eq((param.getLastLoginTime() != null), StoreOrganizationUsers::getLastLoginTime, param.getLastLoginTime());
            Page<StoreOrganizationUsers> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(storeOrganizationUsersService.getPage(page, queryWrapper, key));
        } catch (NumberFormatException e) {
            log.error("分页查询租户机构用户异常", e);
            return Response.failed("分页查询租户机构用户异常");
        }

    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询店铺机构用户
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询店铺机构用户")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(storeOrganizationUsersService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增店铺机构用户")
    public Response saveStoreOrganizationUsers(@RequestBody @Valid StoreOrganizationUsersAddParam param) {
        StoreOrganizationUsers entity;
        try {
            entity = BeanUtils.covertType(param, StoreOrganizationUsers.class);
        } catch (Exception e) {
            log.error("新增租户机构用户Bean转换异常", e);
            return Response.failed("新增租户机构用户Bean转换异常");
        }
        return Response.data(storeOrganizationUsersService.insert(entity));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改店铺机构用户
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改店铺机构用户")
    public Response updateStoreOrganizationUsersById(@RequestBody @Valid StoreOrganizationUsersUpdateParam param) {
        StoreOrganizationUsers entity;
        try {
            entity = BeanUtils.covertType(param, StoreOrganizationUsers.class);
        } catch (Exception e) {
            log.error("修改租户机构用户Bean转换异常", e);
            return Response.failed("修改租户机构用户Bean转换异常");
        }
        return Response.data(storeOrganizationUsersService.updateById(entity));
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 删除店铺机构用户
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除店铺机构用户")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response deleteStoreOrganizationUsersById(@PathVariable("id") Long id) {
        return Response.data(storeOrganizationUsersService.deleteStoreOrganizationUsersById(id));
    }

    /**
     * @Author:campchen
     * @Description: StringUtils.join(ids, " ") 为缓存中添加唯一KEY  如不合适可选择更好方法
     * @Date:上午 10:18 2019/5/11
     * @Param: ids
     * @return: com.hoshiicloud.common.rpc.Response
     */
    @ApiOperation(value = "根据id集合查询店铺机构用户")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.failed("店铺机构用户id集合为空");
        }
        ids.sort(null);
        return Response.data(storeOrganizationUsersService.getByIds(ids, StringUtils.join(ids, "")));
    }


}
